<template>
  <section class="main flex">
    <cartTopBar />
    <emtyCart />
    <cartBody />
  </section>
</template>
<script>
import $ from "../../node_modules/jquery/dist/jquery";
import cartTopBar from "../components/cartTopBar";
import emtyCart from "../components/emtyCart";
import cartBody from "../components/cartBody";
export default {
  components: { cartTopBar, emtyCart, cartBody },

  beforeRouteEnter (to, from, next) {
    if (to.fullPath == "/cart") {
      if (localStorage.checked) {
        let token;
        if (localStorage.checked == "false") {
          token = sessionStorage.token;
        } else {
          token = localStorage.token;
        }
        if (token != undefined) {
          $.ajax({
            type: "get",
            url: "/user/checktoken",
            data: { token },
            dataType: "json",
            success: response => {
              if (response.msg == "已登录") {
                next();
              } else {

                next("/login")
              }
            }
          });
        } else {
          sessionStorage.redirect = "#/cart";

          next("/login")
        }
      } else {
        sessionStorage.redirect = "#/cart";

        next("/login")
      }
    }
  },
  mounted () {
    let id;
    if (localStorage.checked == "false") {
      id = sessionStorage.id;
    } else if (localStorage.checked == "true") {
      id = localStorage.id;
    }
    $.ajax({
      type: "post",
      url: "/goods/cart",
      data: { type: "get", id },
      dataType: "json",
      success: response => {
        for (let i = 0; i < response.data.length; i++) {
          response.data[i].type = true;
        }
        this.$store.state.cartlist = response.data;
      }
    });
  },
  beforeRouteLeave (to, from, next) {
    this.$store.state.carttype = true;
    next();
  }
};
</script>
<style scoped>
.flex {
  display: flex;
  flex-flow: column;
}
</style>